package com.youlu.campus.web.notify;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.common.exception.SFException;

import com.youlu.campus.entity.VO.req.ShunFengOrderVO;
import com.youlu.campus.entity.VO.req.ShunFengRequestVO;
import com.youlu.campus.entity.VO.res.SFCallbackRes;
import com.youlu.campus.service.notify.ShunFengService;
import com.yuelin.infrastructure.quantum.common.QResult;
import javafx.beans.binding.ObjectBinding;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class ShunFengController {

    @Autowired
    private ShunFengService shunFengService;

    @PostMapping("/public/shunfeng/order")
    public QResult shunFengOrder(@RequestBody ShunFengRequestVO requestBody){
        log.info("订单->{} 请求体", JSON.toJSONString(requestBody));
        ShunFengOrderVO shunFengOrder = new  ShunFengOrderVO();
        shunFengOrder.setOrderId(requestBody.getOrderId());
        shunFengOrder.setExpressType(requestBody.getExpressType());
        shunFengOrder.setDContact(requestBody.getD_contact());
        shunFengOrder.setDCompany(requestBody.getD_company());
        shunFengOrder.setDCity(requestBody.getD_city());
        shunFengOrder.setDCounty(requestBody.getD_county());
        shunFengOrder.setDAddress(requestBody.getD_address());
        shunFengOrder.setDProvince(requestBody.getD_province());
        shunFengOrder.setRemark(requestBody.getRemark());
        shunFengOrder.setDMobile(requestBody.getD_mobile());
        shunFengOrder.setCargoList(requestBody.getCargoList());
        shunFengOrder.setJAddress(requestBody.getJ_address());
        shunFengOrder.setJMobile(requestBody.getJ_mobile());
        shunFengService.shunFengOrder(shunFengOrder);
        return QResult.success(null);
    }

    @PostMapping("/public/sf/callback")
    public Map<String,String> sfCallback(@RequestBody Map<String, Object> params){
        log.info("顺丰请求回掉接口 sfCallback  json - >{}",JSON.toJSONString(params));
        Map<String,String> result = new HashMap<>(2);
        try {
            SFCallbackRes sfCallbackRes = JSON.parseObject(JSON.toJSONString(params), SFCallbackRes.class);
            shunFengService.callback(sfCallbackRes);
            result.put("return_msg","接收成功");
            result.put("return_code","0000");
            return result;
        }catch (SFException e){
            log.error("error",e);
            result.put("return_msg",e.getError().getMessage());
            result.put("return_code","1000");
            return result;
        }catch (Exception e){
            log.error("error",e);
            result.put("return_msg","系统错误，请联系我们");
            result.put("return_code","1000");
            return result;
        }
    }








}



